<?php

namespace app\model\v1\recovery;

use app\model\user\User;
use app\model\v1\recruit\RecruitPersonageAuthentication;
use constants\ProductConstants;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\Model;

class RecoveryShop extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
   //     'create_time','update_time','delete_time'
    ];



    const SHOP_TYPE = 1;  //回收入驻
    const PRODUCT_TYPE = 2;  //搬家入驻


    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid');
    }


    /**
     * 回收/搬运入驻管理列表
     * @param $param
     * @return array
     */
    public static function getRecoverySettleList($param = [])
    {
        $param['page'] =  $param['page'] ?? 1;
        $param['limit'] =  $param['limit'] ?? 10;
        $order = ["id"=>"desc"];

        $data['data'] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
            case when type=1 then '回收入驻'
                when type=2 then '搬运入驻'
                end as type_text,
            case when status=1 then '待审核'
                when status=2 then '通过'
                when status=3 then '不通过'
                end as status_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param["type"])) $query->where("type",  $param["type"]);
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("reg_type",'like', '%'.$param["word"].'%');
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data['count'] = self::field ("*")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param["type"])) $query->where("type",  $param["type"]);
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("reg_type",'like', '%'.$param["word"].'%');
            })
            ->count();

        if(!empty($data['data'])){
            foreach ($data['data'] as $key => &$val){
                $val["recovery_type_text"] = RecoveryQuestionCate::getCateTextByIds($val["recovery_type"]);
            }
        }

        return $data;
    }



    public static function getRecoverySettleDetail($param = [])
    {
        $detail = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
            case when type=1 then '回收入驻'
                when type=2 then '搬运入驻'
                end as type_text,
            case when status=1 then '待审核'
                when status=2 then '通过'
                when status=3 then '不通过'
                end as status_text
            ")
            ->where("id", $param["id"])
            ->find();

        return $detail;
    }


    public static function auditRecoverySettle($param = [])
    {
        $find = self::where("id", $param['id'])->find();
        if(empty($find)) return -1;

        $update = ['status' => $param["status"]];
        if(!empty($param["audit_desc"])) $update["audit_desc"] = $param["audit_desc"];

        $res = self::where("id", $param['id'])->update($update);

        /*
        if($param["status"] == 3){
            event('user.recovery', [$find['uid'], $find['title'], $param["audit_desc"]]);
        } */
        return $res;
    }


    /**
     * 回收/搬运入驻管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableRecoverySettle($param = [])
    {
        $status = self::where("id", $param['id'])->value("on_sale");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['on_sale' => $enable]);
        return $msg;
    }





}